﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace MutiMenu
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private NotifyIcon TrayIcon;
        private System.Windows.Forms.ContextMenu notifyiconMnu;
        public List<string> pathList = new List<string>(8);
        public List<string> defaultPathList = new List<string>(8);
        public MainWindow()
        {
            InitializeDefaultPathList();
            InitializePathList();
            InitializeComponent();
            Initializenotifyicon();
            SetAllImage();
            SetEnvironmentVariables();
        }
        private void InitializeDefaultPathList()
        {
            if (pathList.Count > 0)
                pathList.Clear();
            defaultPathList.Add(@"explorer.exe");
            defaultPathList.Add(@"C:\Program Files\Internet Explorer\iexplore.exe");
            defaultPathList.Add(@"C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE");
            defaultPathList.Add(@"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE");
            defaultPathList.Add(@"C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE");
            defaultPathList.Add(@"calc.exe");
            defaultPathList.Add(@"mspaint.exe");
            defaultPathList.Add(@"C:\Program Files\KuGou\KuGou2008\KuGoo.exe");

        }
        public void InitializePathList()
        {
            if(pathList.Count > 0)
                pathList.Clear();
            //get environment variables
            string strEnvMunu = null;
            strEnvMunu = Environment.GetEnvironmentVariable("Frank_MutiMenu", EnvironmentVariableTarget.User);

            if (strEnvMunu == null)
            {
                foreach (string strPath in defaultPathList)
                {
                    pathList.Add(strPath);
                }
            }
            else
            {
                string[] strPathes = Regex.Split(strEnvMunu, ";");
                int length = strPathes.Length < 8 ? strPathes.Length : 8;
                for (int index = 0; index < length;  index ++)
                {
                    pathList.Add(strPathes[index]);
                }

            }
            
            parsePathList();
            
        }
        public void SetEnvironmentVariables()
        {
            StringBuilder envirs = new StringBuilder();
            string envirMenu = "";
            foreach (string path in pathList)
            {
                envirs.Append(path);
                envirs.Append(";");
            }
            envirMenu = envirs.ToString();
            envirMenu = envirMenu.Substring(0, envirMenu.Length - 1);
            Environment.SetEnvironmentVariable("Frank_MutiMenu", envirMenu, EnvironmentVariableTarget.User);
        }
        //add the path in front of the exe name. 
        private void parsePathList()
        {
            string strPath = null;
            strPath = Environment.ExpandEnvironmentVariables("%Path%");
            string[] defaultPathFolder = Regex.Split(strPath, ";");
            List<string> temList = new List<string>(8);
            foreach (string strName in pathList)
            {
                for (int index = 0; index < defaultPathFolder.Length; index++)
                {
                    if (strName.IndexOf(":") >= 0)
                    {
                        temList.Add(strName);
                        break;
                    }
                    StringBuilder path = new StringBuilder();
                    path.Append(defaultPathFolder[index]);
                    path.Append("\\");
                    path.Append(strName);
                    if(File.Exists(path.ToString()))
                    {
                        temList.Add(path.ToString());
                        break;
                    }
                    else if((index + 1) == defaultPathFolder.Length)
                    {
                        temList.Add("");
                    }
                }
            }
            pathList.Clear();
            foreach (string pathName in temList)
            {
                pathList.Add(pathName);
            }
        }
        private void Initializenotifyicon()
        {
            Assembly assem = Assembly.GetExecutingAssembly();
            Stream stream = assem.GetManifestResourceStream(
             "MutiMenu." + "Tray.ico");

            TrayIcon = new NotifyIcon();
            TrayIcon.Icon = new Icon(stream);

            TrayIcon.Text = "MutiMenu "  + "Copyright: Frank Zhao";
            TrayIcon.Visible = true;
            TrayIcon.Click += new System.EventHandler(this.click);

            //tray menu
            System.Windows.Forms.MenuItem[] mnuItms = new System.Windows.Forms.MenuItem[5];
            mnuItms[0] = new System.Windows.Forms.MenuItem();
            mnuItms[0].Text = "Aways Top";
            mnuItms[0].Click += new System.EventHandler(this.TopAllmost);
            mnuItms[0].Checked = true;

            mnuItms[1] = new System.Windows.Forms.MenuItem("-");

            mnuItms[2] = new System.Windows.Forms.MenuItem();
            mnuItms[2].Text = "Settings...";
            mnuItms[2].Click += new System.EventHandler(this.Settings);

            mnuItms[3] = new System.Windows.Forms.MenuItem("-");

            mnuItms[4] = new System.Windows.Forms.MenuItem();
            mnuItms[4].Text = "Exit";
            mnuItms[4].Click += new System.EventHandler(this.ExitSelect);
            mnuItms[4].DefaultItem = true;


            notifyiconMnu = new System.Windows.Forms.ContextMenu(mnuItms);
            TrayIcon.ContextMenu = notifyiconMnu;
        }
        public void SetAllImage()
        {
            SetButtonIcon(pathList[0], Image1);
            SetButtonIcon(pathList[1], Image2);
            SetButtonIcon(pathList[2], Image3);
            SetButtonIcon(pathList[3], Image4);
            SetButtonIcon(pathList[4], Image5);
            SetButtonIcon(pathList[5], Image6);
            SetButtonIcon(pathList[6], Image7);
            SetButtonIcon(pathList[7], Image8);
            
        }
        public void SetButtonIcon(string exePath, System.Windows.Controls.Image image)
        {
            try
            {
                Icon icon = null;
                if (!File.Exists(exePath))
                {
                    Assembly assem = Assembly.GetExecutingAssembly();
                    Stream stream = assem.GetManifestResourceStream(
                        "MutiMenu." + "DefaultIcon.ico");
                    icon = new Icon(stream);
                }
                else
                {
                    icon = ExeIcon.GetLargeIcon(exePath);
                }
                Bitmap bitmap = icon.ToBitmap();
                //MemoryStream stream = new MemoryStream();
                //bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                //ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
                //Image1.Source = (ImageSource)imageSourceConverter.ConvertFrom(stream);

                ImageSource imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                   bitmap.GetHbitmap(),
                   IntPtr.Zero,
                   System.Windows.Int32Rect.Empty,
                   BitmapSizeOptions.FromEmptyOptions());

                image.Source = imageSource;
            }
            catch
            {
            }
        }
        public void click(object sender, System.EventArgs e)
        {
            this.Activate();
        }
        public void TopAllmost(object sender, System.EventArgs e)
        {
            try
            {
                System.Windows.Forms.MenuItem item = (System.Windows.Forms.MenuItem)sender;
                bool check = item.Checked;
                item.Checked = !check;
                this.Topmost = item.Checked;
            }
            catch
            {
            }
        }
      

        public void ExitSelect(object sender, System.EventArgs e)
        {
            TrayIcon.Visible = false;
            this.Close();
        }

        public void Settings(object sender, System.EventArgs e)
        {
            //set the topmost to false if it is true
            bool top = this.Topmost;
            if (top)
            {
                this.Topmost = false;
            }
            SettingDiag setDiag = new SettingDiag(this);
            setDiag.ShowDialog();
            if (top)
            {
                this.Topmost = top;
            }
        }



        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                TrayIcon.Visible = false;
                this.Close();
            }


        }

        private void EllipseModel1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartRun(0);

        }

        private void EllipseModel2_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartRun(1);

        }

        private void EllipseModel3_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartRun(2);

        }

        private void EllipseModel4_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartRun(3);

        }

        private void EllipseModel5_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartRun(4);

        }

        private void EllipseModel6_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartRun(5);

        }

        private void EllipseModel7_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartRun(6);

        }

        private void EllipseModel8_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartRun(7);

        }
        private void StartRun(int index)
        {
            if (!File.Exists(pathList[index]))
                return;
            if (Image1.Opacity == 0)
                return;
            if (pathList[index].IndexOf(@"explorer.exe") >= 0)
            {
                ProcessStartInfo processInfo = new ProcessStartInfo();
                processInfo.FileName = pathList[index];
                processInfo.Arguments = @",/root,";
                processInfo.CreateNoWindow = true;
                Process.Start(processInfo);
            }
            else
            {
                Process.Start(pathList[index]);
            }
        }

        private void Border_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            double opacity = Image1.Opacity;
            Image1.Opacity = 1 - opacity;
            Image2.Opacity = 1 - opacity;
            Image3.Opacity = 1 - opacity;
            Image4.Opacity = 1 - opacity;
            Image5.Opacity = 1 - opacity;
            Image6.Opacity = 1 - opacity;
            Image7.Opacity = 1 - opacity;
            Image8.Opacity = 1 - opacity;

        }
    }
}
